package com.lwy.qiuchao.safety.service.impl;

import com.lwy.common.utils.R;
import com.lwy.qiuchao.safety.entity.SafetyCabinetEntity;
import com.lwy.qiuchao.safety.feign.FieldServiceFeign;
import com.lwy.qiuchao.safety.feign.MemberServiceFeign;
import com.lwy.qiuchao.safety.feign.OrderServiceFeign;
import com.lwy.qiuchao.safety.vo.BoxOrderVo;
import com.lwy.qiuchao.safety.vo.MemberInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.safety.dao.SafetyBoxDao;
import com.lwy.qiuchao.safety.entity.SafetyBoxEntity;
import com.lwy.qiuchao.safety.service.SafetyBoxService;


@Service("safetyBoxService")
public class SafetyBoxServiceImpl extends ServiceImpl<SafetyBoxDao, SafetyBoxEntity> implements SafetyBoxService {

    @Autowired
    SafetyBoxDao safetyBoxDao;

    @Autowired
    FieldServiceFeign fieldServiceFeign;

    @Autowired
    OrderServiceFeign orderServiceFeign;

    @Autowired
    MemberServiceFeign memberServiceFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SafetyBoxEntity> page = this.page(
                new Query<SafetyBoxEntity>().getPage(params),
                new QueryWrapper<SafetyBoxEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void lock(Long fieldId, Long scId, Long mId, Long sbId) {
        SafetyBoxEntity safetyBoxEntity = safetyBoxDao.selectOne(new QueryWrapper<SafetyBoxEntity>().eq("field_id", fieldId).and(obj -> {
            obj.eq("sc_id", scId);
        }).and(obs -> {
            obs.eq("sb_id", sbId);
        }));
        if (safetyBoxEntity != null) {
            safetyBoxEntity.setMId(mId);
            Date date = new Date();
            safetyBoxEntity.setCreateTime(date);
            safetyBoxEntity.setStatus(0);
            safetyBoxDao.updateById(safetyBoxEntity);
        }
        // TODO 给箱子发出指令打开箱门
    }

    @Override
    public Integer unLock(Long fieldId, Long scId, String pwd, Long sbId) {
        SafetyBoxEntity safetyBoxEntity = safetyBoxDao.selectOne(new QueryWrapper<SafetyBoxEntity>().eq("field_id", fieldId).and(obj -> {
            obj.eq("sc_id", scId);
        }).and(obs -> {
            obs.eq("sb_id", sbId);
        }));

        if (safetyBoxEntity != null) {
            // TODO 远程调用
            MemberInfoVo memberInfo = memberServiceFeign.getMemberInfo(safetyBoxEntity.getMId());
            if (memberInfo != null) {
                String passWord = memberInfo.getPhone() + sbId + "";
                if (passWord.equals(pwd)) {
                    return 1;
                }
            }
        }
        return 0;
    }

    @Override
    public void finish(Long mId) {

        SafetyBoxEntity entity = safetyBoxDao.selectOne(new QueryWrapper<SafetyBoxEntity>().eq("m_id", mId));

        if (entity != null) {
            // TODO 1、生成订单 结束计时 远程调用
            BoxOrderVo boxOrderVo = new BoxOrderVo();
            Date createTime = entity.getCreateTime();
            boxOrderVo.setMemberId(mId);
            boxOrderVo.setUseTime(System.currentTimeMillis() - createTime.getTime());
            String fieldName = fieldServiceFeign.getDetailInfoEntity(entity.getFieldId());
            boxOrderVo.setFieldName(fieldName);
            R r = orderServiceFeign.confirmBoxOrder(boxOrderVo);

            if (r.getcode() != 0){
                throw new RuntimeException("生成订单失败");
            }
            // 2、清除箱子信息
            entity.setStatus(1);
            entity.setMId(0L);
            safetyBoxDao.updateById(entity);

        }

    }


}